package com.eunion.boot.settings.datasource.impl;

import java.beans.PropertyVetoException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.alibaba.druid.pool.DruidDataSource;
import com.eunion.boot.settings.datasource.IDruidDataSource;
import com.eunion.boot.settings.datasource.IEnvConfig;

/**
 * 生产环境.
 * @since 2017年3月1日
 */
@Component
@Profile("pro")
@Order(1)
public class DruidDataSourcePro implements IDruidDataSource {

	private final Logger log = LoggerFactory.getLogger(DruidDataSourcePro.class);

	@Autowired
	private IEnvConfig envConfig;

	// @Bean
	public DruidDataSource dataSource()
	    throws PropertyVetoException {
		log.info("Production driverClassName ： {}", envConfig.getDriverClassName());
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setUrl(envConfig.getUrl());
		dataSource.setUsername(envConfig.getUsername());// 用户名
		dataSource.setPassword(envConfig.getPassword());// 密码
		dataSource.setDriverClassName(envConfig.getDriverClassName());
		dataSource.setInitialSize(envConfig.getInitSize());
		dataSource.setMaxActive(envConfig.getMaxSize());
		dataSource.setMinIdle(envConfig.getMinSize());
		dataSource.setMaxWait(60000);
		dataSource.setValidationQuery("SELECT 1");
		dataSource.setTestOnBorrow(false);
		dataSource.setTestWhileIdle(true);
		dataSource.setPoolPreparedStatements(false);
		return dataSource;
	}

}
